package VerticalPrototypes;

import java.sql.*;

/**
 * @author Tyler
 */

//Class for returning an item to the owner; item switches inventory and status.
public class ReturnItem {
    
    //Instance of this class being used.
    private static ReturnItem instance = null;
    
    public ReturnItem()
    {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        //Hardcoded database location.
        String url = "snyamathi.dyndns.org:37965";
        String user = "root";
        String password = "baconatmidnight";
        
        //Hardcoded prototype userIDs and itemID.
        int user_id1 = 1; //Actual owner.
        int user_id2 = 2; //Return-er.
        int item_id = 1;
        
        try {
            //Connect to the databse.
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();

            //Lend an item from one user to another.
  
            //Make sure item can be returned from user2 to user1.
            //Tyler's test string: SELECT itemID FROM userinventory WHERE itemID = 1 AND userID = 2 AND status = 201;
            if(rs == (st.executeQuery("SELECT itemID FROM userinventory WHERE itemID = " + item_id + " AND userID = " + user_id2 + " AND status = 201")))
            {   //Make sure the book is going to it's proper owner.
                
            }
		//Tyler's test string: SELECT itemID FROM userinventory WHERE itemID = 1 AND userID = 1 AND status = 102;
                if(rs == st.executeQuery("SELECT itemID FROM userinventory WHERE itemID = " + item_id + " AND userID = " + user_id1 + " AND status = 102"))
                {
			//Good idea to add a sanity check here? Where: 
                    //FALSE -> Item IDs don't match - item cannot be returned.
                    //TRUE -> Update (return) item status from User2 (back) to User1
                        
                    //update the owner first.
					//Tyler's test string: UPDATE userinventory SET status=101 WHERE itemID = 1 AND userID = 1;
                    if(rs == st.executeQuery("UPDATE userinventory SET status=101 WHERE itemID = " + item_id + " AND userID = " + user_id1))
                    {   //Now update the Borrower.
						//Tyler's test string: UPDATE userinventory SET status=202 WHERE itemID = 1 AND userID = 2;
                        rs = st.executeQuery("UPDATE userinventory SET status=202 WHERE itemID = " + item_id + " AND userID = " + user_id2);
                    }
          
            }
            else
            {
                //Can't lend out a book from owner to borrow.
            }

            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
                //Something happens.
        } finally {
            //Close connections safely - May not need to do this? Overkill?
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                    //Something happens.
            }
        }
    }
    
    //Execute the return of item to an owner.
    public static ReturnItem user_ReturnItem()
    {
        if(instance == null)
            instance = new ReturnItem();
        return instance;
    }  
}